
from common.logOutput import info,error,case

import requests

class BusinessRequest:

    @staticmethod
    def post(url, sid, user_id, body, headers=None):
        if headers is None:
            headers = {
                'Cookie': f'wps_sid={sid}',
                'X-user-key': str(user_id),
                'Content-Type': 'application/json'
            }

        info(f'request url: {url}')
        info(f'request headers: {headers}')
        info(f'request body: {body}')
        try:
            res = requests.post(url, headers=headers, json=body, timeout=5)
        except requests.exceptions.Timeout as e:
            # 专门捕获请求超时异常
            error(f'url:{url}, request timeout!')
            return None  # 抛出自定义异常
        except requests.exceptions.RequestException as e:
            # 捕获其他所有 requests 异常（如连接错误、HTTP 错误等）
            error(f"Request failed: {str(e)}")
            return None  # 抛出自定义异常
        info(f'expect response code:{res.status_code}')
        info(f'expect response body:{res.text}')
        return res #返回Response 对象

    @staticmethod
    def get(url, sid,  headers=None):
        if headers is None:
            headers = {  # 每次调用时生成新的默认 headers
                'Cookie': f'wps_sid={sid}'
            }
        info(f'request url:{url}')
        info(f'request headers:{headers}')
        try:
            res = requests.get(url, headers=headers, timeout=6)
        except requests.exceptions.Timeout as e:
            # 专门捕获请求超时异常
            error(f'url:{url}, request timeout!')
            return None  # 抛出自定义异常
        except requests.exceptions.RequestException as e:
            # 捕获其他所有 requests 异常（如连接错误、HTTP 错误等）
            error(f"Request failed: {str(e)}")
            return None  # 抛出自定义异常
        info(f'expect response code:{res.status_code}')
        info(f'expect response body:{res.text}')
        return res  # 返回Response 对象
